﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayerHealth : MonoBehaviour
{

    private int HP = 0;
    public int HPMax = 100;

    private GameManager m_gameManager;
    private Text m_HPText;
    private Animator m_animator;

    private void Awake()
    {
        m_gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
        m_HPText = GameObject.Find("HP").GetComponent<Text>();
        m_animator = GetComponentInChildren<Animator>();
    }

    public void ReloadAnimator()
    {
        m_animator = GameObject.Find("Body2").GetComponent<Animator>();
    }

    void Start()
    {
        Init();
    }

    public void TakeDamage()
    {
        if (HP > 0)
        {
            HP -= Random.Range(10, 20);
            m_HPText.text = "HP: " + HP + "/" + HPMax;
            if (HP <= 0)
            {
                Dead();
            }
        }
    }

    private void Dead()
    {
        m_animator.SetTrigger("Death");
        Camera.main.transform.position += new Vector3(0f, 3f, 0f);
        Camera.main.transform.rotation = Quaternion.Euler(90f, 0f, 0f);
        StartCoroutine(m_gameManager.GameOver());
    }

    public void Init()
    {
        HP = HPMax;
        m_HPText.text = "HP: " + HP + "/" + HPMax;
    }
}
